home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / WANDR330.ZIP / SRC / CONVERT.C next >
Encoding:
C/C++ Source or Header  |  1990-07-27  |  7.8 KB  |  332 lines

  1. #include "wand_head.h"
  2. #include <errno.h>
  3.  
  4. /*
  5.     This program converts V2.* save files into V3.* save files.
  6.  
  7.     It can do this for compressed/ uncompressed files, and defaults
  8.     to the def in wand_head.h
  9.  
  10.     Usage:
  11.  
  12.         convert [ -v ] [ -C | -N ] [ -c | -n ] oldfile newfile
  13.  
  14.     -v:  verbose
  15.     -C etc : force encryption / no encryption on OLDFILE or newfile.
  16.  
  17. */
  18.  
  19. void Usage(char *);
  20.  
  21. char screen[NOOFROWS][ROWLEN+1];
  22. char screen_name[61];
  23. char *infile, *outfile;
  24.  
  25. struct saved_game {
  26.     short num;
  27.     long  score;
  28.     short bell;
  29.     short maxmoves;
  30.     short num_monsters;
  31. };
  32.  
  33. struct save_vars zz;
  34. struct old_save_vars yy;
  35.  
  36. struct mon_rec *last_of_list, *start_of_list, *tail_of_list;
  37.  
  38. int o_enc, n_enc;
  39. int verbose;
  40.  
  41. crypt_file(name)
  42. char *name;
  43. {
  44.     char buffer[1024];
  45.     int fd, length, loop;
  46.  
  47. #ifdef TOS
  48.     if ((fd = open(name,O_RDONLY)) < 0) {
  49. #else
  50.     if ((fd = open(name,O_RDONLY)) == -1) {
  51. #endif
  52.     sprintf(buffer,"Wanderer: cannot open %s",name);
  53.     perror(buffer);
  54.     exit(1);
  55.     }
  56.     if ((length = read(fd,buffer,1024)) < 1) {
  57.     sprintf(buffer,"Wanderer: read error on %s",name);
  58.     perror(buffer);
  59.     exit(1);
  60.     }
  61.     close(fd);
  62.  
  63. /* Right, got it in here, now to encrypt the stuff */
  64.  
  65.     srand(BLURFL);
  66.     for (loop = 0; loop < length; loop++)
  67.     buffer[loop] ^= rand();
  68.  
  69. #ifdef TOS
  70.     if ((fd = open(name,O_WRONLY|O_TRUNC)) < 0) {
  71. #else
  72.     if ((fd = open(name,O_WRONLY|O_TRUNC))== -1) {
  73. #endif
  74.     sprintf(buffer,"Wanderer: cannot write to %s",name);
  75.     perror(buffer);
  76.     exit(1);
  77.     }
  78.     if (write(fd,buffer,length) != length) {
  79.     sprintf(buffer,"Wanderer: write error on %s",name);
  80.     perror(buffer);
  81.     exit(1);
  82.     }
  83.     close(fd);
  84.  
  85. /* ok, file now contains encrypted/decrypted game. */
  86. /* lets go back home... */
  87. }
  88.  
  89. struct mon_rec *make_monster(x,y)
  90. int x, y;
  91. {
  92.     char *malloc();
  93. #define MALLOC (struct mon_rec *)malloc(sizeof(struct mon_rec))
  94.     struct mon_rec *monster;
  95.     if (tail_of_list->next == NULL) {
  96.     if ((last_of_list = MALLOC) == NULL)
  97.         return NULL;
  98.     tail_of_list->next = last_of_list;
  99.     last_of_list->prev = tail_of_list;
  100.     last_of_list->next = NULL;
  101.     }
  102.     monster = tail_of_list = tail_of_list->next;
  103.     monster->x = x;
  104.     monster->y = y;
  105.     monster->mx = 1;    /* always start moving RIGHT. (fix later)  */
  106.     monster->my = 0;
  107.     monster->under = ' ';
  108.     return monster;
  109. }
  110.  
  111. void save_game(num, score, bell, maxmoves)
  112. int num, bell, maxmoves;
  113. long score;
  114. {
  115.     char fname[128], buf[70], *fp;
  116.     FILE *fo;
  117.     struct saved_game s;
  118.     struct mon_rec *mp;
  119.  
  120.     fp = outfile;
  121.  
  122.     if ((FILE *)NULL == (fo = fopen(outfile, W_BIN))) {
  123.     perror(fp);
  124.     exit(1);
  125.     }
  126.  
  127.     s.num = num;
  128.     s.score = score;
  129.     s.bell = bell;
  130.     s.maxmoves = maxmoves;
  131.     s.num_monsters = 0;
  132.  
  133.     mp = start_of_list;        /* first entry is dummy    */
  134.     while (mp != tail_of_list) {
  135.     mp = mp->next;
  136.     s.num_monsters++;    /* count them monsters    */
  137.     }
  138.  
  139.     if ((1 != fwrite((char *)&s, sizeof(s), 1, fo)) ||
  140.     (1 != fwrite((char *)screen, sizeof(screen), 1, fo)) ||
  141.     (1 != fwrite((char *)&zz, sizeof(zz), 1, fo))) {
  142.         printf("Write error on '%s'\n", fname);
  143.         fclose(fo);
  144.         unlink(fname);
  145.         exit(1);
  146.     }
  147.  
  148.     mp = start_of_list;
  149.     while (mp != tail_of_list) {
  150.     /* save them monsters    */
  151.     mp = mp->next;
  152.     if (1 != fwrite((char *)mp, sizeof(struct mon_rec), 1, fo)) {
  153.         printf("Write error on '%s'\n", fname);
  154.         fclose(fo);
  155.         unlink(fname);
  156.         exit(1);
  157.     }
  158.     }
  159.     fwrite(screen_name, sizeof(char), strlen(screen_name), fo);
  160.     fclose(fo);
  161.     if (n_enc)
  162.         crypt_file(outfile);   /* encrpyt the saved game */
  163.     printf("Game saved.\n\nWANDERER Copyright (C) 1988 S. Shipway\n\n");
  164. }
  165.  
  166. void restore_game(num, score, bell, maxmoves)
  167. int num, bell, maxmoves;
  168. long score;
  169. {
  170.     FILE *fi;
  171.     struct saved_game s;
  172.     struct mon_rec *mp, *tmp, tmp_monst;
  173.     char fname[128], *fp;
  174.  
  175.     fp = infile;
  176.  
  177.     if (o_enc)
  178.        crypt_file(infile);   /* decrypt it */
  179.     if ((FILE *)NULL == (fi = fopen(infile, R_BIN))) {
  180.     printf("Open error on '%s'\n", fp);
  181.     printf("Cannot restore game --- sorry.\n");
  182.     exit(1);
  183.     }
  184.     if ((1 != fread((char *)&s, sizeof(s), 1, fi)) ||
  185.     (1 != fread((char *)screen, sizeof(screen), 1, fi)) ||
  186.     (1 != fread((char *)&yy, sizeof(yy), 1, fi))) {
  187.     printf("Read error on '%s'\n", fp);
  188.     printf("Cannot restore game --- sorry.\n");
  189.     fclose(fi);
  190.     exit(1);
  191.     }
  192.  
  193.     num = s.num;
  194.     score = s.score;
  195.     bell = s.bell;
  196.     maxmoves = s.maxmoves;
  197.  
  198.     /* free any monsters already on chain, to start clean */
  199.     mp = start_of_list->next;
  200.     while ((mp != NULL) && (mp != start_of_list)) {
  201.     /* free them monsters    */
  202.     tmp = mp;
  203.     mp = mp->next;
  204.     free(tmp);
  205.     }
  206.  
  207.     /* re-initialize the monster list    */
  208.     /* *start_of_list = {0,0,0,0,0,NULL,NULL}; */
  209.     start_of_list->x = 0;
  210.     start_of_list->y = 0;
  211.     start_of_list->mx = 0;
  212.     start_of_list->my = 0;
  213.     start_of_list->under = 0;
  214.     start_of_list->next = (struct mon_rec *)NULL;
  215.     start_of_list->prev = (struct mon_rec *)NULL;
  216.  
  217.     tail_of_list = start_of_list;
  218.  
  219.     while (s.num_monsters--) {
  220.     /* use make_monster to allocate the monster structures    */
  221.     /* to get all the linking right without even trying    */
  222.     if ((struct mon_rec *)NULL == (mp = make_monster(0, 0))) {
  223.         printf("Monster alloc error on '%s'\n", fp);
  224.         printf("Try again - it might work.\nBut then,pigs might fly...\n");
  225.         fclose(fi);
  226.         exit(1);
  227.     }
  228.     if (1 != fread((char *)&tmp_monst, sizeof(struct mon_rec), 1, fi)) {
  229.         printf("Monster read error on '%s'\n", fp);
  230.         printf("Cannot restore game --- sorry.\n");
  231.         fclose(fi);
  232.         exit(1);
  233.     }
  234.     /* copy info without trashing links    */
  235.     mp->x     = tmp_monst.x;
  236.     mp->y     = tmp_monst.y;
  237.     mp->mx    = tmp_monst.mx;
  238.     mp->my    = tmp_monst.my;
  239.     mp->under = tmp_monst.under;
  240.     }
  241.     fclose(fi);
  242. }
  243.  
  244. extern int optind;
  245. extern char *optarg;
  246.  
  247. main(argc, argv)
  248. int argc;
  249. char **argv;
  250. {
  251.     int num, bell, maxmoves;
  252.     long score;
  253.     char c;
  254.     struct mon_rec mlist;
  255.  
  256.     start_of_list = &mlist;
  257.  
  258.     verbose = 0;
  259. #ifdef NO_ENCRYPTION
  260.     o_enc = n_enc = 0;
  261. #else
  262.     o_enc = n_enc = 1;
  263. #endif
  264.  
  265.     while ((c = getopt(argc,argv,"CNcnv")) != -1)
  266.     switch (c) {
  267.     case 'C': o_enc = 1; break;
  268.     case 'c': n_enc = 1; break;
  269.     case 'N': o_enc = 0; break;
  270.     case 'n': n_enc = 0; break;
  271.     case 'v': verbose = 1; break;
  272.     default : Usage(argv[0]);
  273.     }
  274.  
  275.     if ((argc - optind) < 2) Usage(argv[0]);
  276.  
  277.     infile = argv[optind++]; outfile = argv[optind];
  278.     if (verbose) printf("Converting %s to %s.\n", infile, outfile);
  279.  
  280.     if (verbose ) {
  281.     printf("Reading in file %s. ", infile);
  282.     if (o_enc) printf("(encrypted)");
  283.     printf("\n");
  284.     }
  285.  
  286.     restore_game(num, score, bell, maxmoves);
  287.  
  288.     if (verbose) printf("Giving screen name.\n");
  289.  
  290.     sprintf(screen_name,"------ Wanderer version %s ------",VERSION);
  291.  
  292.     if (verbose) printf(" Copying struct variables... \n");
  293.  
  294.     zz.z_x = yy.z_x;
  295.     zz.z_y = yy.z_y;
  296.     zz.z_tx = yy.z_tx;
  297.     zz.z_ty = yy.z_ty;
  298.     zz.z_mx = yy.z_mx;
  299.     zz.z_my = yy.z_my;
  300.     zz.z_sx = yy.z_sx;
  301.     zz.z_sy = yy.z_sy;
  302.     zz.z_diamonds = yy.z_diamonds;
  303.     zz.z_nf = yy.z_nf;
  304.  
  305.     if (verbose ) {
  306.     printf( "Saving to file %s. ", outfile);
  307.     if (n_enc) printf("(encrypted)");
  308.     printf("\n");
  309.     }
  310.  
  311.     save_game(num, score, bell, maxmoves);
  312.  
  313.     printf("Done.\n");
  314. #ifdef TOS
  315.     if (!strcmp(argv[0], "")) {    /* probably run from the desktop */
  316.     printf("Press any key to end...");
  317.     fflush(stdout);
  318.     c = console_read_byte(stdin);
  319.     }
  320. #endif    /* TOS */
  321. }
  322.  
  323. void Usage(name)
  324. char *name;
  325. {
  326.     printf("Usage: %s [ -v ] [ -C | -c ] [ -N | -n ] oldfile newfile\n",name);
  327.     printf("-v : verbose\n");
  328.     printf("-C : file is encrypted\n-N : file not encrypted\n");
  329.     printf("Upper case -- old file, lower case -- new file\n");
  330.     exit(1);
  331. }
  332.